package com.lzy.cloud.upload.controller;

import com.lzy.framework.common.request.BaseRequest;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DateUtil;
import com.lzy.framework.core.utils.FtpUtil;
import com.lzy.framework.core.utils.ImageUtils;
import com.lzy.framework.core.utils.QiniuUtils;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created with IntelliJ IDEA.
 * Description: lzy-cloud-service
 *
 * @author: ellengou
 * @date: 2018/8/16/016
 * @time: 15:52
 */
@RestController
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    DozerBeanMapper mapper;

    @GetMapping(value = "/token/{bucketName}")
    public String uploadTestImage(@PathVariable("bucketName") String bucketName) {
        Ensure.that(bucketName).isNotNull("00250001");
       String token =  QiniuUtils.getUpToken(bucketName);
        return token;
    }

    @RequestMapping(value = "/image", method = RequestMethod.POST)
    public String uploadTestImage(@RequestParam(value = "image", required = false) MultipartFile image) {
        Ensure.that(image).isNotNull("00000008");
        String path = QiniuUtils.upload(image, QiniuUtils.QINIU_BUCKET_DOMAIN_ENUM.TEST);
        return path;
    }

    @RequestMapping(value = "/goods", method = RequestMethod.POST)
    public String uploadGoodsImage(@RequestParam(value = "image", required = false) MultipartFile file) {
        Ensure.that(file).isNotNull("00000008");
        String path = QiniuUtils.upload(file, QiniuUtils.QINIU_BUCKET_DOMAIN_ENUM.GOODS);
        return path;
    }

    @RequestMapping(value = "/icon", method = RequestMethod.POST)
    public String uploadIcon(@RequestParam(value = "image", required = false) MultipartFile file) {
        Ensure.that(file).isNotNull("00000008");
        String path = QiniuUtils.upload(file, QiniuUtils.QINIU_BUCKET_DOMAIN_ENUM.ICON);
        return path;
    }

    @RequestMapping(value = "/order", method = RequestMethod.POST)
    public String uploadOrder(@RequestParam(value = "image", required = false) MultipartFile file) {
        Ensure.that(file).isNotNull("00000008");
        String path = QiniuUtils.upload(file, QiniuUtils.QINIU_BUCKET_DOMAIN_ENUM.ORDER);
        return path;
    }

    /**
     * 此方法非七牛存储  （指定存储空间） ，现阶段不可以使用
     * @param file
     * @return
     */
    @RequestMapping(value = "/base", method = RequestMethod.POST)
    public String uploadBase(@RequestParam(value = "image", required = false) MultipartFile file) {
        Ensure.that(file).isNotNull("00000008");
        String path ="";
        try {
            path =  FtpUtil.uploadFile(file.getOriginalFilename(),file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return path;
    }

    @RequestMapping(value = "/activity", method = RequestMethod.POST)
    public String uploadActivity(@RequestParam(value = "image", required = false) MultipartFile file) {
        Ensure.that(file).isNotNull("00000008");
        String path = QiniuUtils.upload(file, QiniuUtils.QINIU_BUCKET_DOMAIN_ENUM.ACTIVITY);
        return path;
    }

    @RequestMapping(value = "/office", method = RequestMethod.POST)
    public String uploadOffice(@RequestParam(value = "image", required = false) MultipartFile file) {
        Ensure.that(file).isNotNull("00000008");
        String path ="";
        try {
            path = QiniuUtils.uploadFile(file.getInputStream(), QiniuUtils.QINIU_BUCKET_DOMAIN_ENUM.BASE, DateUtil.getCurrentMillisecond() + "." + QiniuUtils.parseSuffix(file.getOriginalFilename()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return path;
    }

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public String testZipImage(String img) {
//        Ensure.that(file).isNotNull("00000008");
        String path = null;
        String imagePath = ImageUtils.base64StringToImage(img);
        if (img !=null) {
            try {
                InputStream input = new FileInputStream(imagePath);
                path =  QiniuUtils.upload(input, QiniuUtils.QINIU_BUCKET_DOMAIN_ENUM.ICON);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return path;
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Result deleteImage(@RequestBody BaseRequest file) {
        Ensure.that(file).isNotNull("00000010");
        String fileName = file.getName();
        if (fileName.contains("http://") || fileName.contains("https://")) {
            if (fileName.contains("?"))
                fileName = fileName.substring(0, fileName.lastIndexOf("?"));
            fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
        }
        try {
            QiniuUtils.delete(QiniuUtils.QINIU_GOODS_BUCKET, fileName);
        } catch (Exception e) {
            e.printStackTrace();
            Ensure.that(e).isNull("00000011");
        }
        return new Result();
    }


}

